<template>
  <van-sticky>
    <van-nav-bar title="猫眼电影" placeholder @click="changeHot" />
    <div class="tabs">
      <div>
        <van-tabs
          v-model:active="active"
          line-width="90px"
          title-active-color="rgb(214, 67, 67)"
          @change="changeHot"
        >
          <van-tab
            title="正在热映"
            title-style="fontSize:16px;fontWeight: 700;"
            :to="{ name: 'moviehot' }"
          ></van-tab>
          <van-tab
            title="即将下架"
            title-style="fontSize:16px;fontWeight: 700;"
            :to="{ name: 'movielist' }"
          ></van-tab>
        </van-tabs>
      </div>
      <van-icon name="video-o" />
    </div>
  </van-sticky>
  <router-view></router-view>
  <div v-show="listShow" class="content">
    <div class="hot">
      <p>最受好评的电影</p>
      <div class="bannerList">
        <div
          class="bannerInfo"
          v-for="item in randomHots"
          :key="item"
          @click="goDetail(item)"
        >
          <div class="bannerImg">
            <img :src="item.poster" alt="" />
            <p>观众评分 {{ item.grade }}</p>
          </div>
          <h3 class="van-ellipsis">{{ item.name }}</h3>
        </div>
      </div>
    </div>
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in randomList" :key="item" @click="goDetail(item)">
        <div class="movieList">
          <div class="movieImg">
            <img :src="item.poster" alt="" />
          </div>
          <div class="movieInfo">
            <div class="movieFn">
              <div class="movieName">
                <p class="van-ellipsis">{{ item.name }}</p>
                <van-tag mark type="primary" color="#4f98d1">{{
                  item.item.name
                }}</van-tag>
              </div>
              <div>
                观众评分: <span class="pingfe">{{ item.grade }}</span>
              </div>
              <div>
                主演:
                <span v-for="actors in item.actors.slice(1, 4)" :key="actors"
                  >{{ actors.name }}&nbsp;
                </span>
              </div>
              <div>{{ item.category.split('|').join(' ') }}</div>
            </div>
            <div class="moviebtn">购票</div>
          </div>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>

<script setup>
import { onMounted, ref, computed } from 'vue'
import { useStore } from 'vuex'
import { getMovieList } from '@/api/movie.js'
import { useRouter, useRoute } from 'vue-router'
const store = useStore()

//跳转
const router = useRouter()
const route = useRoute()
const goDetail = (item) => {
  store.commit('movieData/movieInfoUpdate', item),
    router.push({
      name: 'moviedetail',
      params: {
        id: item.filmId
      }
    })
}
//头部功能
const active = ref(0)
//这里必须要用computer 用ref 会不更新
let listShow = computed(() => store.state.movieData.listShow)

const changeHot = () => {
  store.commit('movieData/listShowCut', true)
  onLoad()
}
//骨架屏 功能
onMounted(() => {
  store.commit('movieData/loadingUpdate', false)
  console.log(randomList)

  // console.log(store)
  getMovieList().then((res) => {
    list = res.data.data.films
    randomList.value = list.slice(0)
    randomHots.value = list
    randomHots.value.sort((a, b) => Number(b.grade) - Number(a.grade))
  })
  //这是给子组件准备的
  // store.commit('movieData/listShowCut', true)
  if (route.name === 'moviehot') {
    console.log(active.value)
    store.commit('movieData/listShowCut', true)
    active.value = 0
  }
  if (route.name === 'movielist') {
    console.log('zou le ')
    store.commit('movieData/listShowCut', false)

    active.value = 1
  }
})
//列表功能
let list = []
let randomHots = ref([])
let randomList = ref([])
// let listRefs = toRefs(list)
const loading = ref(false)
const finished = ref(false)

const onLoad = () => {
  setTimeout(() => {
    // randomList.value = randomList.value.concat(list.splice(0, 16))
    // 加载状态结束
    loading.value = false
    // 数据全部加载完成
    if (randomList.value.length >= 32) {
      finished.value = true
    }
  }, 1000)
}
</script>

<style lang="scss" scoped>
.van-nav-bar {
  --van-nav-bar-background-color: rgb(214, 67, 67);
  --van-nav-bar-height: 0.6rem;
  --van-nav-bar-title-font-size: 0.2rem;
  --van-nav-bar-title-text-color: #fff;
}
.tabs {
  display: flex;
  justify-content: flex-end;
  background: #fff;
  .van-icon {
    width: 0.3rem;
    color: rgb(214, 67, 67);
    font-weight: 600;
    font-size: 0.25rem;
    line-height: 0.3rem;
    text-align: center;
    align-self: center;
    margin: 0 0.1rem;
  }
  & > div {
    width: 70%;
  }
}
.content {
  background: #f5f5f5;
  //热评
  .hot {
    margin-bottom: 0.1rem;
    background: #fff;
    padding: 12px 15px 12px 15px;
    font-size: 14px;
    color: #333;
    & > p {
      margin-bottom: 12px;
    }
    .bannerList {
      overflow: scroll;
      white-space: nowrap;
      display: flex;
      .bannerInfo {
        margin-right: 0.1rem;
        width: 1rem;
        h3 {
          color: #222;
          font-size: 0.15rem;
          margin-bottom: 0.03rem;
        }

        .bannerImg {
          width: 1rem;
          height: 1.45rem;
          position: relative;
          margin-bottom: 0.1rem;

          img {
            width: 100%;
            height: 100%;
          }
          p {
            position: absolute;
            bottom: 0rem;
            color: #faaf00;
            font-size: 0.14rem;
            font-weight: 600;
            width: 100%;
            height: 0.35rem;
            background-image: linear-gradient(
              -180deg,
              rgba(77, 77, 77, 0),
              #000
            );
            text-align: center;
            box-sizing: border-box;
            padding-top: 0.15rem;
          }
        }
      }
    }
  }
  .movieList {
    display: flex;
    .movieImg {
      height: 1.1rem;
      width: 0.86rem;
      margin-right: 0.14rem;
      img {
        height: 100%;
        width: 100%;
      }
    }
    .movieInfo {
      display: flex;
      flex: 1;
      .movieFn {
        flex: 1;
        .movieName {
          // width: 100%;
          display: flex;
          max-width: 2.17rem;
          margin-bottom: 0.07rem;
          p {
            font-size: 0.18rem;
            color: #333;
            font-weight: 700;
            margin-right: 0.05rem;
            align-self: center;
          }
          .van-tag {
            flex: none;
            align-self: center;
          }
        }
        .pingfe {
          color: #faaf00;
          font-weight: 700;
          font-size: 0.19rem;
        }
      }
      .moviebtn {
        width: 0.5rem;
        background-color: #f03d37;
        color: #fff;
        border-radius: 0.13rem;
        text-align: center;
        align-self: center;
        height: 0.3rem;
        line-height: 0.3rem;
      }
    }
  }
}
.van-cell {
  border-bottom: 0.01rem solid rgb(250, 250, 250);
}
//滚动条
::-webkit-scrollbar {
  display: none;
}
</style>
